<?php 
/**
s*  @author	kinuz
*  @since	2011. 5. 27
*/ 
namespace MODEL
{
	class UserLogin extends \FX_Model
	{
		public function __construct()
		{
			parent::__construct();
			$this->_init();
		}
		
		private function _init()
		{
			$this->table = 'user_login';
			$this->setOrder("user_login_time DESC");
		}
	    
	    /**
	     * Overriding select
	     */
	    public function s($field=array('*'), $where=array(), $order=array())
	    {
	    	$field = (array) $field;
	    	$where = (array) $where;
	    	$order = (array) $order;
	    	foreach($field as $i=>$f)
	    	{
	    		$field[$i] = '`ul`.'.$field[$i];
	    	}
	    	array_push($field, '`u`.`user_name`');
	    	array_push($field, '`u`.`user_id`');
	    	
	    	$this->setOrder($order);
	    	
	    	$sql = add_comment()."
	    		SELECT
					".implode(',', $field)."
				FROM
					`{$this->table}` AS `ul`
				LEFT JOIN `user` AS `u` ON `ul`.`user_seq` = `u`.`user_seq`
				".(count($where) > 0 ? ' WHERE '.implode(' AND ', $where) : '')."
				{$this->order}
				{$this->limit}
	    	";
			$query = $this->db->query($sql);
			if ($query->num_rows() > 0)
			{
				return $query->result_array();
			}
	    	throw new \NoRecordException("해당 설문조사 참여자가 존재하지 않습니다. :".implode(", ", $where));
	    }
	    
	    /**
	     * override count
	     */
	    public function c($where=array())
	    {
	    	$where = (array) $where;
	    	$sql = add_comment()."
	    		SELECT
					COUNT(*) AS `cnt`
				FROM
					`{$this->table}` AS `ul`
				LEFT JOIN `user` AS `u` ON `ul`.`user_seq` = `u`.`user_seq`
				".(count($where) > 0 ? ' WHERE '.implode(' AND ', $where) : '')."
				LIMIT 0, 1
	    	";
			$query = $this->db->query($sql);
			return (int)$query->first_row()->cnt;	//integer
	    }
	    
	   /**
	     *	Select query for Classes..
		 *  @author	 lingmao
		 *  @since	2011. 5. 31
	     */
	    public function cLoad($user_login_time, $user_seq)
	    {
	    	$sql = add_comment()."
	    		SELECT
					`ul`.* , `u`.`user_name`, `u`.`user_login_id`
				FROM
					`{$this->table}` AS `ul`
				LEFT JOIN `user` AS `u` ON `ul`.`user_seq` = `u`.`user_seq`
				WHERE `user_login_time` = {$this->db->escape($user_login_time)} AND `user_seq` = {$this->db->escape($user_seq)}
				LIMIT 1
	    	";
			$query = $this->db->query($sql);
			if ($query->num_rows() > 0)
			{
				return $query->row_array();
			}
	    	throw new \NoRecordException("설문조사 참여자가 존재하지 않습니다.. :{$user_login_time}, {$user_seq}");
	    }
	}
}